package guoanmaker.operator.business.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorAppBanner;
import guoanmaker.operator.business.model.OperatorBoutiqueRecommend;
import guoanmaker.operator.business.model.OperatorCities;
import guoanmaker.operator.business.model.OperatorFriendshipLink;
import guoanmaker.operator.business.model.OutOperatorBoutiqueRecommend;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ProvideCommodityServiceRange;
import guoanmaker.operator.business.model.ProvideRoom;
import guoanmaker.operator.business.service.OperatorBoutiqueRecommendServiceInterface;
import guoanmaker.operator.business.service.OperatorCitiesServiceInterface;
import guoanmaker.operator.business.service.OperatorRoomServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;
/**
 * 精品推荐
* @ClassName: OperatorBoutiqueRecommendController
* @Description:TODO (这里用一句话描述这个类的作用) 
* @author lihongyu
* @date 2017年8月15日 下午4:59:55 
*  
 */
@RestController
@RequestMapping(ApiController.OPERATOR_BOUTIQUERECOMMEND)
public class OperatorBoutiqueRecommendController {
	@Autowired
	 private  AjaxJson ajaxJson;
	@Autowired
	private OperatorBoutiqueRecommendServiceInterface operatorBoutiqueRecommendServiceInterface;
	@Autowired
	private OperatorCitiesServiceInterface operatorCitiesServiceInterface;
	@Autowired
	private OperatorRoomServiceInterface operatorRoomServiceInterface;
	  @ApiOperation(value = "根据城市code查询精品推荐app", notes = "根据城市code查询精品推荐app")
	    @RequestMapping(value = "/getAllOperatorBoutiqueRecommendByCityCode", method = RequestMethod.POST)
	    public AjaxJson getAllOperatorBoutiqueRecommendByCityCode(String cityCode,Integer page) {
		  Integer size=10;
		  List<OutOperatorBoutiqueRecommend> ofls=operatorBoutiqueRecommendServiceInterface.selectOperatorBoutiqueRecommends(cityCode,page,size);
	    	if(ofls.size()!=0){
	    	
	    		ajaxJson.setSuccess(true).setStatus(200).setData(ofls);
	    	}else{
	    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据");
	    	}
	    		
	    	
	        return ajaxJson;
	    }
	  
	  @ApiOperation(value = "保存精品推荐", notes = "新增/修改精品推荐")
	    @PostMapping(value = "saveAppBoutiqueRecommend")
	    public AjaxJson saveAppBoutiqueRecommend(@RequestBody InRoundPhone inRoundPhone) {
		  OperatorBoutiqueRecommend[] appbrs=inRoundPhone.getOperatorBoutiqueRecommends();
		  operatorBoutiqueRecommendServiceInterface.delectAllBycitycode(inRoundPhone.getCityId());
	    	for(OperatorBoutiqueRecommend appbr:appbrs){
	    		appbr.setCreateDate(new Date());;
	    		appbr.setCityCode(inRoundPhone.getCityId());
	    		if(appbr.getProvideCommodityId().contains("S")){
	    			appbr.setServerType(1);
	    		}else{
	    			appbr.setServerType(2);
	    		}
	    		operatorBoutiqueRecommendServiceInterface.saveOperatorBoutiqueRecommend(appbr);
	    	}
	        return ajaxJson.setSuccess(true).setStatus(200);
	    }
	  
	  
	  @ApiOperation(value = "根据城市code查询精品推荐列表", notes = "根据城市code查询精品推荐列表")
	    @RequestMapping(value = "/getAllOperatorBoutiqueRecommendByCityCodePc", method = RequestMethod.POST)
	    public AjaxJson getAllOperatorBoutiqueRecommendByCityCodePc(String cityCode) {
		 try{
		  List<OperatorBoutiqueRecommend> OperatorBoutiqueRecommends=operatorBoutiqueRecommendServiceInterface.selectOperatorBoutiqueRecommendsPc(cityCode);
	    	
	    	
	    		ajaxJson.setSuccess(true).setStatus(200).setData(OperatorBoutiqueRecommends);
	    	}catch(Exception e){
	    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据").setData(null);
	    	}
	    		
	    	
	        return ajaxJson;
	    }
	  
	  
	  @ApiOperation(value = "校验商品编号是否存在和是否在所在区域", notes = "校验商品编号是否存在和是否在所在区域")
	    @RequestMapping(value = "/getaccAndnumAndcode", method = RequestMethod.POST)
	    public AjaxJson getAllOperatorBoutiqueRecommendByCityCodePc(String provideCommodityId,String cityId) {
		  int a=0;
		
		  String ss="";
		  try{
		 
		  if(provideCommodityId.contains("S")){
			  ProvideRoom provideRoom=operatorRoomServiceInterface.selectProvideRoomByroomCodes(provideCommodityId);
			  if(provideRoom==null){
				  a=1;//商品不存在,请查证后再输/
				  ss="商品不存在,请查证后再输";
			  }else{
			  OperatorCities operatorCities= operatorCitiesServiceInterface.getCitiesByCode(cityId);
			  ProvideRoom pr= operatorRoomServiceInterface.selectProvideRoomByroomCodesAndcityId(provideCommodityId, operatorCities.getId());
			  if(pr==null){
				  a=2;//该商品不在该服务区域
				  ss="该商品不在该服务区域";
			  }
			  }
		  }else{
			  Integer code=Integer.parseInt(provideCommodityId);
			  ProvideCommodity provideCommodity=operatorBoutiqueRecommendServiceInterface.selectProvideCommodity(code);
			  if(provideCommodity==null){
				  a=1;//商品不存在或是空间产品
				  ss="商品不存在或是空间产品";
			  }else{
				  if(!provideCommodity.getIsTerritoryRestriction().equals("2")){//2是全国
					  OperatorCities operatorCities= operatorCitiesServiceInterface.getCitiesByCode(cityId);
					  List<ProvideCommodityServiceRange> serrangs= operatorBoutiqueRecommendServiceInterface.selectProvideCommodityServiceRange(provideCommodity.getId(), operatorCities.getId());
					  if(serrangs.size()==0){
						  a=2;//该商品不在该服务区域
						  ss="该商品不在该服务区域";
					  }
				  }
			  }
		  }
		  
		  ajaxJson.setSuccess(true).setStatus(200).setData(a).setMsg(ss);
	    	}catch(Exception e){
	    		a=3;
	    		ss="系统错误";
	    		ajaxJson.setSuccess(false).setStatus(250).setMsg(ss).setData(a);
	    		
	    	}
	    		
	    	
	        return ajaxJson;
	    }
	  
	  
	  @ApiOperation(value = "根据id删除精品推荐", notes = "根据id删除精品推荐")
	    @RequestMapping(value = "/deleteOperatorBoutiqueRecommendById", method = RequestMethod.POST)
	    public AjaxJson deleteOperatorBoutiqueRecommendById(String id) {
		 try{
			 operatorBoutiqueRecommendServiceInterface.deleteOutOperatorBoutiqueRecommend(id); 
			 ajaxJson.setSuccess(true).setStatus(200).setData(null);
		 }catch(Exception e){
			 ajaxJson.setSuccess(true).setStatus(250).setData(null);
		 }
		 
	    	
	    		
	    	
	        return ajaxJson;
	    }
	  
	  

}
